package d1_reflect;

import org.junit.Test;

import java.lang.reflect.Method;

public class t3method {
    @Test
    public void test1() throws Exception {
        Class c = Student.class;//获取class对象
        Method[] methods = c.getDeclaredMethods();//获取成员方法
        for(Method m : methods){
            System.out.println(m.getName()+"--->"+m.getParameterCount()+"--->"+m.getReturnType());
        }
        System.out.println("-----------------------------------------------------------------------------");
        //获取某个方法对象
        Method run= c.getDeclaredMethod("run");//无参run方法
        System.out.println(run.getName()+"--->"+run.getParameterCount()+"--->"+run.getReturnType());
        Method eat = c.getDeclaredMethod("eat",String.class);
        System.out.println(eat.getName()+"--->"+eat.getParameterCount()+"--->"+eat.getReturnType());

        Student s1= new Student();
        run.setAccessible(true);
        Object rs = run.invoke(s1);//执行方法，并且返回一个object类型的值
        System.out.println(rs);

        String rs2 =(String) run.invoke(s1);
        System.out.println(rs2);
    }
}
